package com.jiazhong.basic.pack0309;

import java.util.*;

// Collection---->List---->ArrayList
public class DemoA {
    // 1. 如何产生对象
    private static void a() {
        ArrayList<Integer> list1 = new ArrayList<>();
        // 符合规范的
        List<String> list2 = new ArrayList<>();
        // 以一个旧集合复制产生新集合
        List<String> list3 = new ArrayList<>(list2);
        System.out.println(list1);
        System.out.println(list2);
        System.out.println(list3);
    }

    // 2. 如何对集合进行CRUD(存放、获取、修改、删除)
    private static void b() {
        List<String> list = new ArrayList<>();
        list.add("aa");
        list.add("bb");
        list.add("cc");
        list.add("aa");
        list.add("dd");
        list.add(2, "kk");
        String s2 = list.get(2);
        System.out.println(s2);
        list.set(1, "mm");
        System.out.println(list);
        list.remove(2);
        list.remove("jj");
        System.out.println(list);
        list.clear();
        System.out.println(list);
    }

    // 3. 其他方法
    private static void c() {
        List<String> list = new ArrayList<>();
        list.add("aa");
        list.add("bb");
        list.add("cc");
        list.add("dd");
        list.add("ee");
        list.add("ff");
        list.add(null);
        System.out.println(list);
        int size = list.size();
        System.out.println(size);
        List<String> stringList = list.subList(1, 4);
        System.out.println(stringList);
        Object[] array = list.toArray();
        System.out.println(Arrays.toString(array));
        boolean empty = list.isEmpty();
        System.out.println(empty);
    }

    // 4. 循环集合
    private static void d() {
        List<Integer> list = new ArrayList<>();
        for (int i = 0; i < 20; i++) {
            list.add((int) (Math.random() * 100 + 1));
        }
        System.out.println(list);
        // 1.for
        for (int i = 0; i < list.size(); i++) {
            Integer e = list.get(i);
            System.out.print(e + "\t");
        }
        System.out.println();
        // 2.foreach缩写版
        for (int e : list) {
            System.out.print(e + "\t");
        }
        System.out.println();
        // 3.foreach标准版
        Iterator<Integer> iterator = list.iterator();
        while (iterator.hasNext()) {
            int e = iterator.next();
            System.out.print(e + "\t");
        }
        System.out.println();
        // 4.lambda
        list.forEach(e -> System.out.print(e + "\t"));
    }

    // 5. 如何排序、如何查找
    private static Scanner scan = new Scanner(System.in);
    private static void e1(){
        List<Integer> list = new ArrayList<>();
        for (int i = 0; i < 20; i++) {
            list.add((int) (Math.random() * 20 + 1));
        }
        System.out.println(list);
        System.out.print("Input:");
        int num = scan.nextInt();
        boolean b = list.contains(num);
        System.out.println(b);
        int index1 = list.indexOf(num);
        int index2 = list.lastIndexOf(num);
        System.out.println(index1);
        System.out.println(index2);
    }
    private static void e2(){
        List<Integer> list = new ArrayList<>();
        for (int i = 0; i < 20; i++) {
            list.add((int) (Math.random() * 20 + 1));
        }
        System.out.println(list);
        Collections.sort(list);
        System.out.println(list);
    }
    public static void main(String[] args) {
        e2();
    }
}
